{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Analyze - Classiq Visualization Tool"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Once a quantum algorithm was designed and a quantum program was synthesized, the next natural thing one would to do is to analyze the quantum program and to check if the underlying quantum circuit implements the desired quantum algorithm properly and how. This can be done with the Classiq visualization tool that enables exploring the quantum circuit interactively on different level of functional hierarchies, as well as to extract crucial information about it.\n",
    "\n",
    "The Classiq visualization tool can be accessed in 3 different ways:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.**From the IDE** - from the `Model` tab, by pressing the `Synthesize` button:\n",
    "<div  style=\"text-align:center;\" >\n",
    "    <img src=\"https://docs.classiq.io/resources/optimize_default.png\" style=\"width:100%;\">\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.**From the Python SDK** - by running the `show(quantum_program)` command in the Python SDK:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:27:47.538205Z",
     "iopub.status.busy": "2024-05-07T13:27:47.536941Z",
     "iopub.status.idle": "2024-05-07T13:27:52.778917Z",
     "shell.execute_reply": "2024-05-07T13:27:52.778255Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Opening: https://platform.classiq.io/circuit/f729dc29-9aa2-49f6-907f-c284445c4dc8?version=0.41.0.dev39%2B79c8fd0855\n"
     ]
    }
   ],
   "source": [
    "from classiq import *\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main(x: Output[QNum], y: Output[QNum]):\n",
    "\n",
    "    allocate(4, x)\n",
    "    hadamard_transform(x)  # creates a uniform superposition\n",
    "    y |= x**2 + 1\n",
    "\n",
    "\n",
    "quantum_program = synthesize(create_model(main))\n",
    "show(quantum_program)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.**Directly Uploading a circuit/ quantum program to the IDE**:\n",
    "<div  style=\"text-align:center;\" >\n",
    "    <img src=\"https://docs.classiq.io/resources/analyze_upload.gif\" style=\"width:100%;\">\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the following, we cover some key aspects of the visualization tool. For a full list of all the features and possibilities of the tool access the reference manual."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Navigating"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "The high-level functional design approach of Classiq enables analyzing quantum circuits on different level of functionalities. You can view your circuit only on the functional building blocks level, e.g. `hadamard_transform` and `Arithmetic` in the example below, or you can zoom in to the actual gate-level implementations:\n",
    "<div  style=\"text-align:center;\" >\n",
    "    <img src=\"https://docs.classiq.io/resources/analyze_buttons.gif\" style=\"width:100%;\">\n",
    "</div>\n",
    "\n",
    "In addition to the plus and minus buttons, you can also use the navigation map button to navigate through your circuit:\n",
    "<div  style=\"text-align:center;\" >\n",
    "    <img src=\"https://docs.classiq.io/resources/analyze_navigation_map.gif\" style=\"width:100%;\">\n",
    "</div>\n",
    "This is especially helpful in large circuits.\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Quantum Program Information "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "On the left-hand side of the visualization tool, you can find information about the quantum program such as the circuit depth, circuit width and the gate count. As you can see in the picture below, it is divided into information about the quantum program the the transpiled quantum circuit:\n",
    "<div  style=\"text-align:center;\" >\n",
    "    <img src=\"https://docs.classiq.io/resources/analyze_program_info.png\" style=\"width:100%;\">\n",
    "</div>\n",
    "\n",
    "The quantum program is the output of the synthesis engine and it contains the circuit implementation **that preserves the functional logic of your algorithm**. This is the information which correlates to the circuit which is viewed on the center of the page. E.g. in the example covered in this tutorial, the final functional building block within the `Power` block and the first functional building block within the `Adder` block should cancel each other as applying them consecutively is equivalent to not applying them at all (these blocks are the quantum Fourier transform and its inverse which cancel each other when applied consecutively):\n",
    "<div  style=\"text-align:center;\" >\n",
    "    <img src=\"https://docs.classiq.io/resources/analyze_program_info_transpilation.png\" style=\"width:100%;\">\n",
    "</div>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "On the other hand, **the transpiled circuit is the circuit that is sent for execution** which is further optimized by eliminating functional blocks that have no influence on the circuit like the two blocks that cancel each other. The transpiled circuit contains only the set of gates defined by the optimization preferences. It is possible to view the transpiled circuit by downloading the circuit `as Transpiled QASM` and uploading it to the visualization tool:\n",
    "<div  style=\"text-align:center;\" >\n",
    "    <img src=\"https://docs.classiq.io/resources/analyze_transpiled_view.png\" style=\"width:100%;\">\n",
    "</div>"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "The Quantum program can be downloaded in various formats including QASM, JPEG, JSON, and LaTeX code. To do this, click the download button as illustrated in the preceding image"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Adapting Names"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In order to keep track of your quantum circuits when you are working, it is recommended to rename them with some indicative names. The renaming is done by double clicking on the circuit tab, entering the new name and confirming.\n",
    "<div  style=\"text-align:center;\" >\n",
    "    <img src=\"https://docs.classiq.io/resources/analyze_renaming.png\" style=\"width:100%;\">\n",
    "</div>\n",
    "\n",
    "By double clicking on the blocks within the circuit, their names can be adapted as well."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "After analyzing the quantum circuit one would usually want to either adapt the model or to execute the circuit on a simulator or on real hardware. Continue to Execute chapter to understand how to do that.\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.11.7 ('classiq')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  },
  "vscode": {
   "interpreter": {
    "hash": "529b62266d4f537a408698cf820854c65fe877011c7661f0f70aa11c4383fddc"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
